<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Grid\AlarmRecover;
use App\Admin\Actions\Grid\AlarmReset;
use App\Admin\Repositories\Alarm;
use App\Models\Alarm as AlarmModel;
use App\Models\Channel as ChannelModel;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class AlarmController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Alarm(), function (Grid $grid) {

            // 禁用部分操作
            $grid->disableCreateButton();
            $grid->disableRowSelector();

            $grid->tools(function (Grid\Tools $tools) {
                $tools->append(AlarmReset::make());
            });

            $grid->model()->with(['channel.platform'])->orderBy('id', 'desc');

            $grid->column('id');
            $grid->column('channel.name', '频率');
            $grid->column('channel.platform.name', '台站');
            $grid->column('source');
            $grid->column('type');
            $grid->column('duration');
            $grid->column('duration')->display(function ($time) {
                return AlarmModel::formatSeconds($time);
            });

            $grid->column('time');
            $grid->column('images', '图片')
                ->display('查看') // 设置按钮名称
                ->modal('图片', function () {
                    if ($this->image) {
                        return view('alarmImage', ['image' => $this->image]);
                    }

                    return '无图片';
                });

            $grid->column('description');
            $grid->column('frequency');
            $grid->column('reason');
            $grid->column('status')->dot([
                '报警' => 'danger',
                '恢复' => 'success',
            ]);

            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableDelete();
                $actions->disableEdit();
                $actions->disableView();

                if ($actions->row->status == '报警') {
                    $actions->append(AlarmRecover::make());
                }
            });

            $grid->filter(function (Grid\Filter $filter) {
                // 报警频率
                $filter->where('channel_id', function ($query) {
                    return $query->where(['channel_id' => (int)$this->input]);
                }, '报警频率')->select(ChannelModel::getNameList());

                // 报警类型
                $filter->like('type');

                // 时间
                $filter->between('time')->datetime();

                // 状态
                $filter->equal('status')->select(['报警' => '报警', '恢复' => '恢复']);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Alarm(), function (Show $show) {
            $show->field('id');
            $show->field('station');
            $show->field('source');
            $show->field('type');
            $show->field('duration');
            $show->field('time');
            $show->field('image');
            $show->field('description');
            $show->field('frequency');
            $show->field('reason');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Alarm(), function (Form $form) {
            $form->display('id');
            $form->text('station');
            $form->text('source');
            $form->text('type');
            $form->text('duration');
            $form->text('time');
            $form->text('image');
            $form->text('description');
            $form->text('frequency');
            $form->text('reason');
            $form->text('status');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
